package org.cqepc.project.reform.service.impl;

import com.alibaba.fastjson2.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.User;
import org.cqepc.project.reform.model.entity.UserEntity;
import org.cqepc.project.reform.service.UserService;
import org.cqepc.project.reform.mapper.UserMapper;
import org.cqepc.project.reform.utils.result.Result;
import org.cqepc.project.reform.utils.util.SerialNoGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
* @author 张鹏
* @description 针对表【reform_user】的数据库操作Service实现
* @createDate 2025-05-14 20:30:52
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserEntity login(String username) {
        QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
        userEntityQueryWrapper.eq("username", username);

        return userMapper.selectOne(userEntityQueryWrapper);
    }

    @Override
    public List<UserEntity> getUsers() {
        return userMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public UserEntity updateUser(UserEntity user) {
        QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
        userEntityQueryWrapper.eq("user_id", user.getUserId());

        int update = userMapper.update(user, userEntityQueryWrapper);

        if (update == 1) {
            return userMapper.selectOne(userEntityQueryWrapper);
        }

        return null;
    }

    @Override
    public UserEntity addUser(UserEntity user) {
        int insert = userMapper.insert(user);
        if (insert == 1) {
            QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
            userEntityQueryWrapper.eq("user_id", user.getUserId());

            return userMapper.selectOne(userEntityQueryWrapper);
        }
        return null;
    }

    @Override
    public Integer deleteUser(String userId) {
        QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<>();
        userEntityQueryWrapper.eq("user_id", userId);

        return userMapper.delete(userEntityQueryWrapper);
    }

    @Override
    public String getMaxRoundCode() {
        return userMapper.getMaxRoundCode();
    }
}




